<template>
  <el-row :gutter="16" class="panel-group">
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <el-card class="box-card">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-international">
            <svg-icon icon-class="international" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              文章数量
            </div>
            <count-to :start-val="0" :end-val="statistics.articleTotal"  :duration='3000' class="card-panel-num"/>
          </div>
        </div>
      </el-card>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <el-card class="box-card">
          <div class="card-panel">
            <div class="card-panel-icon-wrapper icon-shopping">
              <svg-icon icon-class="shopping" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                标签数量
              </div>
              <count-to :start-val="0" :end-val="statistics.tagsTotal"  :duration='3000' class="card-panel-num"/>
            </div>
          </div>
        </el-card>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <el-card class="box-card">
          <div class="card-panel">
            <div class="card-panel-icon-wrapper icon-people">
              <svg-icon icon-class="peoples" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                分类数量
              </div>
              <count-to :start-val="0" :end-val="statistics.categoryTotal"  :duration='3000' class="card-panel-num"/>
            </div>
          </div>
        </el-card>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <el-card class="box-card">
          <div class="card-panel">
            <div class="card-panel-icon-wrapper icon-example">
              <svg-icon icon-class="example" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                友情链接数量
              </div>
              <count-to :start-val="0" :end-val="statistics.linksTotal" class="card-panel-num"/>
            </div>
          </div>
        </el-card>
    </el-col>
  </el-row>
  <el-row :gutter="20" style="margin-top:1.25rem">
    <el-col :xs="24" :sm="24" :lg="12">
      <el-card>
        <div class="e-title">文章分类统计</div>
        <div class="chart-wrapper">
<!--          <div id="categoryChart" class="chart" style="height:310px;width:100%" />-->
          <div ref="chartContainer" style="width: 600px; height: 400px;"></div>
        </div>
      </el-card>
    </el-col>

    <!-- 文章标签统计 -->
    <el-col :xs="24" :sm="24" :lg="12">
      <el-card style="height: 417px">
        <div class="e-title">文章标签统计</div>
        <div>
          <div>
             <div ref="chart" style="width: 600px; height: 400px;"></div>
          </div>
        </div>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup name="IndexAdmin">
import {ref,onMounted} from "vue";
import countTo  from '@/components/count-to'
import {getInfo, initCategory} from "@/api/indexAmin";
import * as d3 from 'd3-cloud';
//  按需引入 echarts
import * as echarts from "echarts";
import B2wordcloud from "b2wordcloud";
const chartContainer = ref(null);
const chartInstance = ref(null);
const categoryList = ref(null);
const result = ref(null);
const tagsList = ref(null);
const chart = ref(null);
const version = ref('3.8.6')
const statistics = ref({
  articleTotal: null,
  tagsTotal: null,
  categoryTotal: null,
  linksTotal: null,
})

const  getTotalNumOfCaller = function() {
  getInfo().then(response=>{
    statistics.value = response.data
  });
}
const  getCategoryNumber = function() {
  initCategory().then(response=>{
    result.value = response.data.result;
    categoryList.value = response.data.categoryList;
    tagsList.value = response.data.tagsList;
    // 初始化 ECharts 实例
    chartInstance.value = echarts.init(chartContainer.value);
    createWordCloud();
    // 配置图表选项
    const options = {
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b} : {c} ({d}%)'
      },
      legend: {
        left: 'center',
        bottom: '10',
        data: categoryList.value
      },
      series: [
        {
          name: '类别名称',
          type: 'pie',
          roseType: 'radius',
          radius: [15, 95],
          center: ['50%', '38%'],
          data: result.value,
          animationEasing: 'cubicInOut',
          animationDuration: 2600
        }
      ],
    };
    // 使用刚指定的配置项和数据显示图表
    chartInstance.value.setOption(options);
    return {
      chartContainer,
    };
  });
}

//初始化分类统计
onMounted(() => {
  getCategoryNumber();
});

const createWordCloud = () => {
  const wordCloud = new B2wordcloud(chart.value, {
    list: tagsList.value,
    minFontSize: 30, // 设置字体大小为30
    shape: 'circle', // 设置形状为圆形
    width: 500,     // 设置宽度为 500
    height: 500,    // 设置高度为 500
    tooltip: {
      show: true,
      formatter: function(item) {
        return '<div>' + item[0] + '</div>' + '<div>' + item[1] + '</div>'
      }
    },
    shadowColor: 'rgba(0,0,0,0.9)',
    shadowOffsetX: 2,
    shadowOffsetY: 2,
    shadowBlur: 5,
    color: [
      ['#f00', '#ff0'],
      '#f00',
      'green',
      '#333',
      'blue',
      ['blue', 'white']
    ]
  });
  return {
    chart
  };
};

getTotalNumOfCaller();

</script>
<style lang="scss" scoped>
.tag-cloud {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.tag-cloud > div {
  margin: 5px;
}
.card-panel-num {
  font-size: 20px;
}
.panel-group {
  .card-panel-col {
    margin-bottom: 16px;
  }

  .card-panel {
    height: 80px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    //background: #fff;
    //box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-international {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #b6a2de;
      }
      .icon-example {
        background: #2ec7c9;
      }
      .icon-component {
        background: #8d98b3;
      }
      .icon-pdf {
        background: #e5cf0d;
      }
      .icon-monitor {
        background: #ffb980;
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-international {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #b6a2de;
    }
    .icon-example {
      color: #2ec7c9;
    }
    .icon-component {
      color: #8d98b3;
    }
    .icon-pdf {
      color: #e5cf0d;
    }
    .icon-monitor {
      color: #ffb980;
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 6px 0 0 6px;
      padding: 12px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 15px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
